package com.sheledon.flyCat.network.socket.server;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author sheledon
 */
@Slf4j
public class HttpServer {
    private static final int PORT = 8080;
    private int core = 100;
    private int max = 200;
    private int liveTime =60;
    public void start()  {

        ThreadPoolExecutor executor =
                new ThreadPoolExecutor(core, max, liveTime, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));
        ServerSocket server ;
        try {
            server = new ServerSocket(PORT);
            Socket socket ;
            log.info("server start at port {}",PORT);
            while((socket=server.accept())!=null){
                log.info("accept request {}",socket.getRemoteSocketAddress());
                executor.execute(new HttpServerHandler(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}
